home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-07-14 | 5.7 KB | 198 lines | [TEXT/CWIE] |
- // ===========================================================================
- // String_Utils.cp ©1993 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- //
- // String manipulation utility functions
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __SCRIPT__
- #include <Script.h>
- #endif
-
- #include "String_Utils.h"
- #include "Common.h"
-
-
- // ---------------------------------------------------------------------------
- // • CopyPStr
- // ---------------------------------------------------------------------------
- // Copy a Pascal string
- //
- // Returns a pointer to the copied string
- //
- // inDestSize specifies the maximum size of the destination (INCLUDING
- // the length byte). The default is sizeof Str255.
- //
- // By calling this function as:
- //
- // CopyPStr(source, dest, sizeof(dest));
- //
- // you can change the declaration of dest without having to
- // remember to change the call.
-
- StringPtr
- CopyPStr(
- ConstStr255Param inSourceStr,
- StringPtr outDestStr,
- SInt16 inDestSize)
- {
- #if DEBUG
- UInt32 dbgSrcFirst = (UInt32) inSourceStr;
- UInt32 dbgSrcLast = dbgSrcFirst + inSourceStr[0];
- UInt32 dbgDestFirst = (UInt32) outDestStr;
- UInt32 dbgDestLast = dbgDestFirst + inDestSize - 1;
- #endif
-
- WARNING(inSourceStr != nil, "CopyPStr inSourceStr == nil");
- WARNING(outDestStr != nil, "CopyPStr outDestStr == nil");
- WARNING(inDestSize > 0, "CopyPStr outDestStr <= 0");
- WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
- "CopyPStr strings overlapped");
-
- SInt16 dataLen = inSourceStr[0] + 1;
- if (dataLen > inDestSize) {
- dataLen = inDestSize;
- }
-
- ::BlockMoveData(inSourceStr, outDestStr, dataLen);
- outDestStr[0] = dataLen - 1;
- return outDestStr;
- }
-
- // ---------------------------------------------------------------------------
- // • ConcatPStr
- // ---------------------------------------------------------------------------
- // Concatenate two Pascal strings. The first string becomes the combination
- // of the first and second strings.
- //
- // Returns a pointer to the concatenated string
- //
- // inDestSize specifies the maximum size of the concatenated string
- // (INCLUDING the length byte). The default is sizeof Str255.
- //
- // By calling this function as:
- //
- // ConcatPStr(io, appendMe, sizeof(io));
- //
- // you can change the declaration of io without having to
- // remember to change the call.
-
- StringPtr
- ConcatPStr(
- Str255 ioFirstStr,
- ConstStr255Param inSecondStr,
- SInt16 inDestSize)
- {
- #if DEBUG
- UInt32 dbgSrcFirst = (UInt32) ioFirstStr;
- UInt32 dbgSrcLast = dbgSrcFirst + inDestSize - 1;
- UInt32 dbgDestFirst = (UInt32) inSecondStr;
- UInt32 dbgDestLast = dbgDestFirst + inSecondStr[0];
- #endif
-
- WARNING(ioFirstStr != nil, "ConcatPStr ioFirstStr == nil");
- WARNING(inSecondStr != nil, "ConcatPStr inSecondStr == nil");
- WARNING(inDestSize > 0, "ConcatPStr inDestSize <= 0");
- WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
- "ConcatPStr strings overlapped");
-
- // Limit combined string to inDestSize chars
- SInt16 charsToCopy = inSecondStr[0];
- if (ioFirstStr[0] + charsToCopy > inDestSize - 1) {
- charsToCopy = inDestSize - 1 - ioFirstStr[0];
- }
-
- // Copy second to end of first string
- ::BlockMoveData(inSecondStr + 1, ioFirstStr + ioFirstStr[0] + 1,
- charsToCopy);
- // Set length of combined string
- ioFirstStr[0] += charsToCopy;
-
- return ioFirstStr;
- }
-
-
- Boolean PStr_Identical(ConstStr255Param inStringOne, ConstStr255Param inStringTwo)
- {
- WARNING(inStringOne != nil, "PStr_Identical inStringOne == nil");
- WARNING(inStringTwo != nil, "PStr_Identical inStringTwo == nil");
-
- UInt32 itr;
-
- for(itr = 0; itr <= inStringOne[0]; itr++)
- {
- // found a difference return false
- if (inStringOne[itr] != inStringTwo[itr])
- { return false; }
- }
-
- return true;
- }
-
- #if 0
- // ---------------------------------------------------------------------------
- // • CopyPStrIntl
- // ---------------------------------------------------------------------------
- // Copy a Pascal string
- //
- // Returns a pointer to the copied string
- //
- // inDestSize specifies the maximum size of the destination (INCLUDING
- // the length byte). The default is sizeof Str255.
- //
- // By calling this function as:
- //
- // CopyPStr(source, dest, sizeof(dest));
- //
- // you can change the declaration of dest without having to
- // remember to change the call.
- StringPtr CopyPStrIntl(ConstStr255Param inSourceStr, StringPtr outDestStr,
- SInt16 inDestSize)
- {
- WARNING(0, "CopyPStrIntl has not been debugged yet do not use w/o doing so");
-
- #if DEBUG
- UInt32 dbgSrcFirst = (UInt32) inSourceStr;
- UInt32 dbgSrcLast = dbgSrcFirst + inSourceStr[0];
- UInt32 dbgDestFirst = (UInt32) outDestStr;
- UInt32 dbgDestLast = dbgDestFirst + inDestSize - 1;
- #endif
-
- WARNING(inSourceStr != nil, "CopyPStr inSourceStr == nil");
- WARNING(outDestStr != nil, "CopyPStr outDestStr == nil");
- WARNING(inDestSize > 0, "CopyPStr outDestStr <= 0");
- WARNING((dbgSrcFirst > dbgDestLast) || (dbgSrcLast < dbgDestFirst),
- "CopyPStr strings overlapped");
-
- short charType;
- SInt16 charsToCopy = inDestSize - 1;
-
- if ( inSourceStr[0] > charsToCopy )
- {
- /* Make sure the string isn't truncated in the middle of */
- /* a multi-byte character. */
- while (charsToCopy != 0)
- {
- charType = CharByte((Ptr)&inSourceStr[1], charsToCopy);
- if ((charType == smSingleByte) || (charType == smLastByte))
- break; /* inSourceStr[charsToCopy] is now a valid last character */
- --charsToCopy;
- }
- }
- else
- {
- charsToCopy = inSourceStr[0];
- }
-
- /* Set the outDestStr string length */
- outDestStr[0] = charsToCopy;
- /* and copy charsToCopy characters */
- ::BlockMoveData(inSourceStr, outDestStr, charsToCopy);
-
- return outDestStr;
- }
-
- #endif